﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ABBService.IDAL;
using ABBService.Models;
using ABBWebService.Robot.ABB;
using Newtonsoft.Json;

namespace ABBService.DAL
{
    public class ABBService : IABBService
    {
        ABBWebApiClient client;
        public ABBService(Model_ABBConfiguration abb)
        {
            client = new ABBWebApiClient(abb.ABBIPAddr, int.Parse(abb.ABBPort), abb.ABBUser, abb.ABBPwd);
        }
        public async Task<string> ABB_CtrlState(Model_ABBConfiguration abb)
        {
            var res = await client.GetCtrlStateAsync();
            if (!res.IsSuccess)
            {
                throw new Exception(res.Message);
            }
            return res.Content;
        }
        public async Task<string> ABB_ErrState(Model_ABBConfiguration abb)
        {
            var r1 = await client.GetErrorStateAsync();
            if (!r1.IsSuccess)
            {
                throw new Exception(r1.Message);
            }
            return r1.Content;
        }
        public async Task<Model_Joints> ABB_Joints(Model_ABBConfiguration abb)
        {
            var r1 = await client.GetJointTargetAsync();
            if (!r1.IsSuccess)
            {
                throw new Exception(r1.Message);
            }
            var r2 = r1.Content?.Replace("[", "").Replace("]", "").Split(",").ToList();

            if (r2 != null && r2.Count == 6)
            {
                Model_Joints joint = new Model_Joints()
                {
                    J4 = r2[0],
                    J5 = r2[1],
                    J6 = r2[2],
                    J1 = r2[3],
                    J2 = r2[4],
                    J3 = r2[5]
                };
                return joint;
            }
            else
            {
                return null;
            }
        }

        public async Task<string> ABB_OperationMode(Model_ABBConfiguration abb)
        {
            var r1 = await client.GetOperationModeAsync();
            if (!r1.IsSuccess)
            {
                throw new Exception(r1.Message);
            }
            return r1.Content;

        }

        public async Task<Model_RapidExcution> ABB_RapidExcution(Model_ABBConfiguration abb)
        {
            var r1 = await client.GetRapidExecutionAsync();

            if (r1.IsSuccess)
            {
                var r2 = JsonConvert.DeserializeObject<Model_RapidExcution>(r1.Content);
                return r2;
            }
            else
            {
                return new Model_RapidExcution() { cycle = "unkown", ctrlexecstate = "unkown" };
            }

        }

        public async Task<List<Model_RapidTasks>> ABB_RapidTasks(Model_ABBConfiguration abb)
        {

            var r1 = await client.GetRapidTasksAsync();
            var r2 = JsonConvert.DeserializeObject<List<Model_RapidTasks>>(r1.Content);

            return r2;

        }

        public async Task<Model_TargetPoints> ABB_RobotTarget(Model_ABBConfiguration abb)
        {

            var r1 = await client.GetRobotTargetAsync();

            return JsonConvert.DeserializeObject<Model_TargetPoints>(r1.Content);

        }

        public async Task<Model_Servoenable> ABB_Servoenable(Model_ABBConfiguration abb)
        {

            var r1 = await client.GetServoEnableAsync();

            var r2 = JsonConvert.DeserializeObject<Model_Servoenable>(r1.Content.Replace("-", "_"));
            return r2;

        }

        public async Task<string> ABB_SpeedRatio(Model_ABBConfiguration abb)
        {

            var r1 = await client.GetSpeedRatioAsync();
            var r2 = r1.Content;

            return r2;

        }
    }
}
